package com.mutual.assistance.user.api;


import com.mutual.assistance.response.R;
import com.mutual.assistance.user.model.dto.RoleDTO;
import com.mutual.assistance.user.model.vo.AddPermissionVO;
import com.mutual.assistance.user.model.vo.AddPermissionsVO;
import com.mutual.assistance.user.model.vo.AddRoleVO;
import com.mutual.assistance.user.model.vo.UpdateRoleVO;
import com.mutual.assistance.user.service.AuthorityRoleService;
import com.mutual.assistance.validation.ValidationGroups;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author chenyuxin
 * @version 1.0
 * @description 角色相关请求
 * @date 2023/4/21 19:30
 */

@RestController
@RequestMapping("/authority")
@Api(value = "角色相关接口", tags = "角色相关接口")
public class AuthorityRoleController {

    @Resource
    private AuthorityRoleService authorityRoleService;

    @PostMapping("/role")
    @ApiOperation("添加一个角色并且为该角色赋予权限")
    @PreAuthorize("hasAuthority('sysmanager_role_add')")
    public R<String> addRoleAndMenu(
            @RequestBody @Validated(ValidationGroups.Insert.class) AddRoleVO addRoleVO
    ) {
        authorityRoleService.addRoleAndMenu(addRoleVO);
        return R.success(null, "添加成功");
    }

    @PostMapping("/role/nomenu")
    @ApiOperation("为角色添加一条非菜单权限")
    @PreAuthorize("hasAuthority('sysmanager_role_permission')")
    public R<String> addOneMenuForRole(
            @RequestBody @Validated(ValidationGroups.Insert.class) AddPermissionVO addPermissionVO
    ) {
        authorityRoleService.addOneMenuForRole(addPermissionVO);
        return R.success(null, "添加成功");
    }

    @PostMapping("/role/menu")
    @ApiOperation("为角色添加一条菜单权限")
    @PreAuthorize("hasAuthority('sysmanager_role_permission')")
    public R<String> addMenuForRole(
            @Validated(ValidationGroups.Insert.class) AddPermissionVO addPermissionVO
    ) {
        authorityRoleService.addMenuForRole(addPermissionVO);
        return R.success(null, "添加成功");
    }

    @PostMapping("/role/menus")
    @ApiOperation("为角色添加多条菜单权限")
    @PreAuthorize("hasAuthority('sysmanager_role_permission')")
    public R<String> addMenusForRole(
            @Validated(ValidationGroups.Insert.class) AddPermissionsVO addPermissionsVO
    ) {
        authorityRoleService.addMenusForRole(addPermissionsVO);
        return R.success(null, "添加成功");
    }


    @DeleteMapping("/role/{roleId}")
    @ApiOperation("删除角色以及角色对应的权限")
    @PreAuthorize("hasAuthority('sysmanager_role_delete')")
    public R<String> deleteRoleAndMenus(@NotBlank(message = "要删除的角色主键不能为空") @PathVariable("roleId") String roleId) {
        authorityRoleService.deleteRoleAndMenus(roleId);
        return R.success(null, "删除成功");
    }

    @DeleteMapping("/roles")
    @ApiOperation("删除多条角色以及角色对应的权限")
    @PreAuthorize("hasAuthority('sysmanager_role_delete')")
    public R<String> deleteRolesAndMenus(
            @NotNull(message = "要删除的角色主键集合不能为空")
            @RequestParam("roleIds") List<String> roleIds) {
        authorityRoleService.deleteRolesAndMenus(roleIds);
        return R.success(null, "删除成功");
    }


    @PutMapping("/role")
    @ApiOperation("更新角色相关信息")
    @PreAuthorize("hasAuthority('sysmanager_role_edit')")
    public R<String> updateRoleAndPermission(
            @RequestBody @Validated(ValidationGroups.Update.class) UpdateRoleVO updateRoleVO
    ) {
        authorityRoleService.updateRoleAndPermission(updateRoleVO);
        return R.success(null, "修改成功");
    }


    @GetMapping("/role/{roleId}")
    @ApiOperation("获取角色相关信息与权限")
    @PreAuthorize("hasAuthority('sysmanager_role_permission')")
    public R<RoleDTO> getRole(
            @NotBlank(message = "要获取的角色主键不能为空") @PathVariable("roleId") String roleId
    ) {
        RoleDTO role = authorityRoleService.getRole(roleId);
        return R.success(role, "获取成功");
    }


    @GetMapping("/role/all")
    @ApiOperation("获取角色相关信息与权限")
    @PreAuthorize("hasAuthority('sysmanager_role_permission')")
    public R<List<RoleDTO>> getAllRole() {
        List<RoleDTO> allRole = authorityRoleService.getAllRole();
        return R.success(allRole, "获取成功");
    }

}
